package com.blog.blog.service.impl;

import com.blog.blog.service.EmailNotificationService;
import com.blog.blog.vo.CommentVO;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

/**
 * 邮件通知服务实现
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class EmailNotificationServiceImpl implements EmailNotificationService {
    
    private final JavaMailSender mailSender;
    private final TemplateEngine templateEngine;
    
    @Value("${spring.mail.username}")
    private String fromEmail;
    
    @Value("${app.site.name:个人博客}")
    private String siteName;
    
    @Value("${app.site.url:http://localhost:3000}")
    private String siteUrl;
    
    @Override
    @Async
    public void sendCommentNotification(String articleTitle, Long articleId, CommentVO comment, String authorEmail) {
        try {
            Context context = new Context();
            context.setVariable("siteName", siteName);
            context.setVariable("siteUrl", siteUrl);
            context.setVariable("articleTitle", articleTitle);
            context.setVariable("articleUrl", siteUrl + "/articles/" + articleId);
            context.setVariable("commenterName", comment.getNickname());
            context.setVariable("commentContent", comment.getContent());
            context.setVariable("commentTime", comment.getCreateTime());
            
            String htmlContent = templateEngine.process("email/comment-notification", context);
            
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
            
            helper.setFrom(fromEmail, siteName);
            helper.setTo(authorEmail);
            helper.setSubject("您的文章《" + articleTitle + "》收到新评论");
            helper.setText(htmlContent, true);
            
            mailSender.send(message);
            log.info("评论通知邮件发送成功: {} -> {}", articleTitle, authorEmail);
            
        } catch (Exception e) {
            log.error("发送评论通知邮件失败: {}", e.getMessage(), e);
        }
    }
    
    @Override
    @Async
    public void sendReplyNotification(String articleTitle, Long articleId, CommentVO replyComment, CommentVO parentComment, String parentCommentEmail) {
        try {
            Context context = new Context();
            context.setVariable("siteName", siteName);
            context.setVariable("siteUrl", siteUrl);
            context.setVariable("articleTitle", articleTitle);
            context.setVariable("articleUrl", siteUrl + "/articles/" + articleId);
            context.setVariable("replierName", replyComment.getNickname());
            context.setVariable("replyContent", replyComment.getContent());
            context.setVariable("originalContent", parentComment.getContent());
            context.setVariable("replyTime", replyComment.getCreateTime());
            
            String htmlContent = templateEngine.process("email/reply-notification", context);
            
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
            
            helper.setFrom(fromEmail, siteName);
            helper.setTo(parentCommentEmail);
            helper.setSubject("您的评论收到新回复");
            helper.setText(htmlContent, true);
            
            mailSender.send(message);
            log.info("回复通知邮件发送成功: {} -> {}", articleTitle, parentCommentEmail);
            
        } catch (Exception e) {
            log.error("发送回复通知邮件失败: {}", e.getMessage(), e);
        }
    }
    
    @Override
    @Async
    public void sendSystemNotification(String to, String subject, String content) {
        try {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(fromEmail);
            message.setTo(to);
            message.setSubject(subject);
            message.setText(content);
            
            mailSender.send(message);
            log.info("系统通知邮件发送成功: {} -> {}", subject, to);
            
        } catch (Exception e) {
            log.error("发送系统通知邮件失败: {}", e.getMessage(), e);
        }
    }
    
    @Override
    @Async
    public void sendWelcomeEmail(String to, String username) {
        try {
            Context context = new Context();
            context.setVariable("siteName", siteName);
            context.setVariable("siteUrl", siteUrl);
            context.setVariable("username", username);
            
            String htmlContent = templateEngine.process("email/welcome", context);
            
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
            
            helper.setFrom(fromEmail, siteName);
            helper.setTo(to);
            helper.setSubject("欢迎加入" + siteName);
            helper.setText(htmlContent, true);
            
            mailSender.send(message);
            log.info("欢迎邮件发送成功: {} -> {}", username, to);
            
        } catch (Exception e) {
            log.error("发送欢迎邮件失败: {}", e.getMessage(), e);
        }
    }
}
